package com.timepies.button;

import android.app.Dialog;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.timepies.DettaglioPieActivity;
import com.timepies.ElencoPieActivity;
import com.timepies.R;

public class PieConfirmButton {

	public static Button addTo(final ElencoPieActivity activity) {

		final Dialog dialog = activity.getEditDialog();
		final Button button = (Button) dialog.findViewById(R.id.confirm_dialog);

		button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Log.d(this.getClass().getSimpleName(), "click confirm");
				EditText testo = (EditText) dialog.findViewById(R.id.testo);
				EditText oggetto = (EditText) dialog.findViewById(R.id.oggetto);
				EditText date = (EditText) dialog.findViewById(R.id.data);

				// || (date.getText().toString().length() == 0)
				if ((date.getText().toString().length() == 0)
						|| (testo.getText().toString().length() == 0)) {
					Toast.makeText(
							activity.getBaseContext(),
							"Devono essere compilati almeno i primi due campi!",
							Toast.LENGTH_LONG).show();
					return;
				}

				try {
					activity.getDb().save(testo.getEditableText().toString(),
							oggetto.getEditableText().toString(),
							date.getEditableText().toString());
					PieListView.addTo(activity);

				} catch (Exception e) {
					Toast.makeText(activity.getBaseContext(), "Errore",
							Toast.LENGTH_LONG).show();
				}
				dialog.dismiss();
			}

		});
		return button;
	}

	public static Button addTo(final DettaglioPieActivity activity) {
		final Dialog dialog = activity.getEditDialog();
		final Button button = (Button) dialog.findViewById(R.id.confirm_dialog);

		EditText testo = (EditText) dialog.findViewById(R.id.testo);
		EditText oggetto = (EditText) dialog.findViewById(R.id.oggetto);
		EditText date = (EditText) dialog.findViewById(R.id.data);
		testo.setText(activity.getPie().getTesto());
		oggetto.setText(activity.getPie().getOggetto());
		date.setText(activity.getPie().getData());

		button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Log.d(this.getClass().getSimpleName(), "click confirm");
				EditText testo = (EditText) dialog.findViewById(R.id.testo);
				EditText oggetto = (EditText) dialog.findViewById(R.id.oggetto);
				EditText date = (EditText) dialog.findViewById(R.id.data);
				try {
					activity.getDb().update(testo.getEditableText().toString(),
							oggetto.getEditableText().toString(),
							date.getEditableText().toString(),
							activity.getPie().getId());

				} catch (Exception e) {
					Toast.makeText(activity.getBaseContext(),
							"Errore" + e.getMessage() + e.getCause(),
							Toast.LENGTH_LONG).show();
				}
				dialog.dismiss();
				activity.exit();
			}

		});
		return button;

	}
}
